home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 0022-3.564 / dmg-3413 / news.txt / pascal4.asc < prev    next >
Text File  |  1989-04-05  |  8KB  |  216 lines

  1.  
  2.      --------------------------------------------------------------
  3.  
  4.      PPPPPP      AAAAA      SSSS S     CCCCC      AAAAA      LL
  5.      PP   PP    AA   AA    SS   SS    CC    C    AA   AA     LL
  6.      PP   PP    AA   AA    SS         CC         AA   AA     LL
  7.      PPPPPP     AAAAAAA     SSSSS     CC         AAAAAAA     LL
  8.      PP         AA   AA         SS    CC         AA   AA     LL
  9.      PP         AA   AA    SS   SS    CC    C    AA   AA     LL
  10.      PP         AA   AA    S SSSS      CCCCC     AA   AA     LLLLLLL
  11.  
  12.      ===============================================================
  13.  
  14.                    TUTORIAL PART 4 - BY DEREK CLEGG
  15.  
  16.      ---------------------------------------------------------------
  17.  
  18.  
  19.      This time I'll be introducing the concept of local and global
  20.      variables, and I'll be expanding a little on parameters.
  21.  
  22.  
  23.  
  24.  
  25.      But first possible solutions to the tasks from last time;
  26.  
  27.  
  28.  
  29.      i/   PROCEDURE Sort_Two_Ints ( X,Y : INTEGER );
  30.           (* X and Y are value parameters! *) 
  31.  
  32.              BEGIN
  33.                IF X > Y 
  34.                     THEN WRITELN ( X,Y," X,Y")
  35.                     ELSE WRITELN ( Y,X," Y,X")
  36.              END;
  37.  
  38.  
  39.  
  40.  
  41.      ii/  PROGRAM Sort_Three_Ints ( INPUT, OUTPUT );
  42.  
  43.                VAR   N1,N2,N3, Temp : INTEGER;
  44.      
  45.                
  46.  
  47.                PROCEDURE Sort_Two_Ints ( VAR X,Y : INTEGER );
  48.                (* X and Y are variable parameters. *)
  49.  
  50.                  BEGIN
  51.                    IF Y < X
  52.                      THEN BEGIN
  53.                             Temp := X;
  54.                             X := Y;
  55.                             Y := Temp
  56.                           END
  57.                  END; 
  58.  
  59.  
  60.  
  61.                BEGIN     (* Start of main program *)
  62.                  READLN ( N1,N2,N3 );
  63.                  Sort_Two_Ints ( N1,N2 );
  64.                  Sort_Two_Ints ( N2,N3 );
  65.                  Sort_Two_Ints ( N1,N2 );
  66.                  WRITLEN ( N1,N2,N3,"  Sorted!!")
  67.                END.
  68.  
  69.                
  70.  
  71.  
  72.  
  73.  
  74.  
  75. iii/      PROGRAM ValidateDate ( Input, Output );
  76.  
  77.                VAR   Days, Month, Year : INTEGER;
  78.                      Ch                : CHAR;
  79.                      Valid             : BOOLEAN;
  80.  
  81.  
  82.  
  83.                PROCEDURE Validate( DD,MM,YY : INTEGER; Valid : BOOLEAN );
  84.  
  85.                     BEGIN
  86.                       IF ( DD > 31 ) OR ( DD < 1 )
  87.                          THEN Valid=FALSE;
  88.                       IF ( MM > 12 ) OR ( MM < 1 )
  89.                          THEN Valid=FALSE;
  90.                       IF ( YY > 1976 ) OR ( YY < 1901 )
  91.                          THEN Valid=FALSE;
  92.  
  93.                       CASE MM OF
  94.                          1 : IF DD <= 31 THEN Valid = TRUE
  95.                                          ELSE Valid = FALSE;
  96.                          2 : BEGIN
  97.                                IF DD > 28 THEN BEGIN
  98.                                                   IF YY MOD 4 = 0 
  99.                                                      THEN Valid = TRUE
  100.                                                      ELSE Valid = FALSE
  101.                                                END
  102.                                           ELSE Valid = TRUE
  103.                              END;
  104.                          3 : IF DD <=31 THEN Valid = TRUE
  105.                                         ELSE Valid = FALSE;
  106.                          4 : IF DD <=30 THEN Valid = TRUE
  107.                                         ELSE Valid = FALSE;
  108.                          5 : IF DD <=31 THEN Valid = TRUE
  109.                                         ELSE Valid = FALSE;
  110.                          6 : IF DD <=30 THEN Valid = TRUE
  111.                                         ELSE Valid = FALSE;
  112.                          7 : IF DD <=31 THEN Valid = TRUE
  113.                                         ELSE Valid = FALSE;
  114.                          8 : IF DD <=31 THEN Valid = TRUE
  115.                                         ELSE Valid = FALSE;
  116.                          9 : IF DD <=30 THEN Valid = TRUE
  117.                                         ELSE Valid = FALSE;
  118.                         10 : IF DD <=31 THEN Valid = TRUE
  119.                                         ELSE Valid = FALSE;
  120.                         11 : IF DD <=30 THEN Valid = TRUE
  121.                                         ELSE Valid = FALSE;
  122.                         12 : IF DD <=31 THEN Valid = TRUE
  123.                                         ELSE Valid = FALSE
  124.                     END
  125.                   END;
  126.  
  127.  
  128.                BEGIN
  129.                   REPEAT
  130.                     BEGIN
  131.                       Valid = TRUE;
  132.                       READLN( Days,Ch,Month,Ch,Year );
  133.                       Validate( Days, Month, Year, Valid );
  134.                       IF Valid
  135.                          THEN WRITELN('This is a valid date!')
  136.                          ELSE WRITELN('Sorry, Invalid date!')
  137.                       ENDIF
  138.                     END;
  139.                   UNTIL Year=0
  140.                END.
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  **** I *think* the above solutions are correct, but am not sure as
  151.       they have not been checked or traced!    The above solution to
  152.       the validate a date problem is very bulky and clumsy as it is 
  153.       easier to solve if an array is used.  When I come to discuss 
  154.       arrays I may use this problem again.
  155.  
  156.       If I had less overtime to do at work this would all have been
  157.      checked and wouldn't have been just thrown together but as 
  158.      somebody once said, "It's a dirty job but somebody's got to do
  159.      it!"
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.                Variable parameters
  169.               ---------------------
  170.  
  171.  
  172.      We have already looked at value parameters, but there is another
  173. parameter know as a variable parameter.  The values of variable
  174. parameters are preserved from one use/call to the next.  Below is a
  175. typical function heading which has two formal parameters, one value
  176. parameter and one variable parameter.
  177.  
  178.  
  179.       FUNCTION Random (VAR seed : INTEGER; max : INTEGER) : INTEGER;
  180.  
  181.  
  182.      Note that ALL variable parameters MUST be preceeded by the keyword
  183. VAR.  Note also that there is no limit to the number of parameters a
  184. function/procedure can have provided that when it is called, the calling
  185. statement has the same number of actual parameters, AND these are listed
  186. in the same order and type as the corresponding formal parameter in the
  187. function heading.
  188.  
  189.      To call the above function, you would need a pascal statement such
  190. as ;
  191.  
  192.           random_no := random( Rseed, 36 );
  193.  
  194.      where 36 is the actual value parameter and Rseed is the name of the
  195. variable ( from the calling part of the program ) that corresponds to 
  196. seed. 
  197.  
  198.      There is no reason why the global variable here represented by 
  199. 'Rseed' should not have been called 'seed' just as it is in the function
  200. heading except that by naming the variable parameter differently aids
  201. readability and makes the program easier to understand though someone
  202. who is just learning programming and has never programmed before may 
  203. get confused by the name change.
  204.  
  205.      In the above example two values are passed to the function.  These
  206. are the values of Rseed and 36.   When the computer has finished 
  207. executing the lines of code which make up this function,  it forgets
  208. about 36 ( ie erases 36 from its memory ) BUT the value of the LOCAL 
  209. variable 'seed' is then assigned to the GLOBAL variable 'Rseed' before
  210. the computer erases 'seed' from its memory and the value of 'seed' also.
  211.  
  212.  
  213.  
  214.                                 ~~~OOOO~~~
  215.  
  216.